package io.github.talelin.latticy.controller.v1;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.bo.BannerBO;
import io.github.talelin.latticy.dto.BannerWithItemsDTO;
import io.github.talelin.latticy.model.Banner;
import io.github.talelin.latticy.service.BannerService;
import io.github.talelin.latticy.common.util.BeanCopyUtil;
import io.github.talelin.latticy.common.util.PageUtil;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.PageResponseVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.Positive;

/**
 * banner控制器
 *
 * @author YiHua
 * @date 2021/11/16 1:10 上午
 */
@Validated
@RestController
@RequestMapping("/v1/banner")
public class BannerController {

     @Autowired
     private BannerService bannerService;         // banner业务层

     /**
      * 创建一个banner
      *
      * @param bannerBO true banner创建的入参信息
      */
     @PostMapping("")
     public CreatedVO create(@RequestBody @Validated BannerBO bannerBO) {
          Banner banner = new Banner();
          BeanCopyUtil.copy(bannerBO, banner);
          // FIXME 这里就是直接调用接口继承的'IService'接口方法
          this.bannerService.save(banner);
          return new CreatedVO();
     }

     /**
      * 删除banner
      *
      * @param id true bannerId
      */
     @DeleteMapping("/{id}")
     public DeletedVO delete(@PathVariable @Positive Integer id) {
          bannerService.delete(id);
          return new DeletedVO();
     }

     /**
      * 修改banner
      *
      * @param bannerBO banner修改的入参信息
      * @param id       bannerId
      */
     @PutMapping("/{id}")
     public UpdatedVO update(@RequestBody @Validated BannerBO bannerBO, @PathVariable @Positive Integer id) {
          bannerService.update(bannerBO, id);
          return new UpdatedVO();
     }

     /**
      * 查询banner信息，并且包含子项信息
      *
      * @param id bannerId true
      */
     @GetMapping("/{id}")
     public BannerWithItemsDTO getWithItems(@PathVariable @Positive Integer id) {
          return bannerService.getWithItems(id);
     }

     /**
      * 查询多个banner,支持分页
      *
      * @param pageSize 每页条数 false
      * @param pageNum  当前页 false
      */
     @GetMapping("/page")
     public PageResponseVO<Banner> getBanners(
         @RequestParam(name = "pageSize", required = false, defaultValue = "10")
         @Min(value = 1, message = "{page.pageSize.min}")
         @Max(value = 30, message = "{page.pageSize.max}") Integer pageSize,
         @RequestParam(name = "pageNum", required = false, defaultValue = "0")
         @Min(value = 0, message = "{page.pageNum.min}") Integer pageNum
     ) {
          // 查询出分页后的banner信息
          IPage<Banner> paging = this.bannerService.paging(pageNum, pageSize);
          // 返回分页数据对象
          return PageUtil.build(paging);
     }
}
